# flask web 网络编程文件
from flask import Flask, render_template, request, escape
from weather import apiweather, xing_key
from vsearch import log
# html 调用方法：render_template  按照 jinja2 规则
#

app = Flask(__name__)

@app.route('/results', methods=['POST','GET'])
# 这些数据来自哪里？

def do_search() -> 'html':
    location = request.form['weather']
    title = '这是你的查询结果:'
    results = str(apiweather(xing_key,location))
    backgroundcolor =request.form['backgroundcolor']
    weather = request.form['weather']
    log(request,results)

    return render_template('results.html',
                           the_title=title,
                           the_backgroundcolor=backgroundcolor,
                           the_weather=results,
                           )
@app.route('/')
@app.route('/hello', methods=['POST','GET'])
def home():
    title = '欢迎~'
    return render_template("hello.html",
                           the_title=title,)

@app.route('/entry', methods=['POST','GET'])
def entry_page() -> 'html':
    title = '欢迎!'
    return render_template('entry.html',

                           the_title=title)


@app.route('/viewlog', methods=['POST','GET'])
def view_the_log() -> 'html':
    """用户日志"""
    contents = []
    with open('viewlog.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('viewlog.html',
                           the_title='日志系统',
                           the_data=contents)


if __name__ == '__main__':
    app.run(debug=True)
